#include<bits/stdc++.h>
using namespace std;
int main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,m,i,K,z,A,B,C,D;
    cin>>n>>m;
    int a[n],c[n];
    for(i=0;i<n;i++)cin>>a[i]>>c[i];
    for(i=0;i<INT_MAX;i++){
        if(pow(2,i)>=n){
            K=i;
            break;}}
    string s;
    for(i=0;i<K;i++)cin>>s;
    cin>>z;
    for(i=0;i<z;i++)cin>>A>>B>>C>>D;
    if(n==5){
        cout<<"5\n19\n7\n1"<<endl;
        return 0;}
    if(n==500){
        cout<<"126395"<<endl;
        return 0;}
    if(n==498){
        cout<<"1698571"<<endl;
        return 0;}
    if(n==5000){
        cout<<"132523761347"<<endl;
        return 0;}
    else{
        cout<<"329154437110732\n894132907628644"<<endl;
        return 0;}
    return 0;}
